using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;
using System.Text;
using System.Data.SqlClient;

public partial class ViewBlog : System.Web.UI.Page
{
    Manager objManager = new Manager(true,"ConnectionString");
    protected string DoctorName = "";
    public string strURlDoctorname = string.Empty;
     public string strURL = string.Empty;
    protected localFunction lfunction = new localFunction();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                if (Request.QueryString["monthid"] != null && Convert.ToBoolean(Convert.ToInt32(Request.QueryString["monthid"])) == false)
                {
                    Response.Redirect(Session["strHttp"] + "/Community/Community1.aspx");
                }
            }
            catch
            {
                Response.Redirect(Session["strHttp"] + "/Community/Community2.aspx");
            }
          
            BindGrid(Convert.ToInt32(Session["userID"].ToString()));
        }
    }
    private void BindGrid(int id)
    {
        //Filling the grid with the messages of the topic selected in the combo
        Manager mgrobj = new Manager(true,"ConnectionString");
        string sql = "";
        ////try
        ////{
            if ((Session["userType"].ToString() == "Doctor") && ((Convert.ToString(Session["DoctorType"])) == "Doctor"))
            {
                Session["BlogType"] = "2";
                if (Request.QueryString["monthid"] != null)
                {
                    sql = "select substring(blog.blogMessage,1,500) as BlogMessage, blog.id as blogid ,blog.blogTitle as BlogTitle,REPLACE(LTRIM(RTRIM(blog.blogTitle)), ' ', '_') as BlogTitleURL,doc.FName+' '+doc.MName+' '+doc.Lname ";
                    sql = sql + "as Name,blog.blogPostedDate   from blog inner join tbl_doctors doc on  blog.doctorID=doc.id ";
                    sql = sql + " where blog.doctorID=" + id + "   and month(blogPostedDate) =" + Convert.ToInt32(Request.QueryString["monthid"]) + " and year(blogPostedDate)=year(getdate())";
                }
                else
                {
                    sql = "select substring(blog.blogMessage,1,500) as BlogMessage, blog.id as blogid ,blog.blogTitle as BlogTitle,REPLACE(LTRIM(RTRIM(blog.blogTitle)), ' ', '_') as BlogTitleURL,doc.FName+' '+doc.MName+' '+doc.Lname ";
                    sql = sql + "as Name,blog.blogPostedDate   from blog inner join tbl_doctors doc on  blog.doctorID=doc.id ";
                    sql = sql + " where blog.doctorID=" + id + " order by blogposteddate desc";

                    //Response.Write(sql);
                }
            }
            else if ((Session["userType"].ToString() == "Doctor") && (Convert.ToString(Session["DoctorType"]) == "Dentist"))
            {
                Session["BlogType"] = "1";
                if (Request.QueryString["monthid"] != null)
                {
                    sql = "select substring(blog.blogMessage,1,500) as BlogMessage ,blog.id as blogid ,blog.blogTitle as BlogTitle,REPLACE(LTRIM(RTRIM(blog.blogTitle)), ' ', '_') as BlogTitleURL,dent.FName+' '+dent.MName+' '+dent.Lname  ";
                    sql = sql + "as Name,blog.blogPostedDate from blog inner join tbl_dentists dent on  blog.doctorID=dent.id ";
                    sql = sql + " where blog.doctorID=" + id + " and month(blogPostedDate) =" + Convert.ToInt32(Request.QueryString["monthid"]) + " and year(blogPostedDate)=year(getdate())";
                }
                else
                {
                    sql = "select substring(blog.blogMessage,1,500) as BlogMessage ,blog.id as blogid ,blog.blogTitle as BlogTitle,REPLACE(LTRIM(RTRIM(blog.blogTitle)), ' ', '_') as BlogTitleURL,dent.FName+' '+dent.MName+' '+dent.Lname  ";
                    sql = sql + "as Name,blog.blogPostedDate from blog inner join tbl_dentists dent on  blog.doctorID=dent.id ";
                    sql = sql + " where blog.doctorID=" + id + " order by blogposteddate desc";

                    //Response.Write(sql);
                }
            }

            if (sql.ToString() != "")
            {
                DataSet ds = mgrobj.ExecuteQuery(sql, "tbl");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DoctorName = ds.Tables[0].Rows[0]["Name"].ToString();
                    strURlDoctorname = ds.Tables[0].Rows[0]["Name"].ToString();
                    strURlDoctorname = strURlDoctorname.ToString().Trim().Replace(" ", "_");
                    //strURL = Session["strHttp"] + "blog/" + strURlDoctorname;
                    strURL = Session["strHttp"] + "blog";
                    grdMessages.DataSource = ds.Tables[0];
                    grdBlogTitle.DataSource = ds.Tables[0];
                    //grdMessages.PageSize = Convert.ToInt32(Session["AdminPageSize"]);
                    grdMessages.DataBind();
                    grdBlogTitle.DataBind();
                }
                else // no topics exist
                {
                    grdMessages.DataSource = ds.Tables[0];
                    grdBlogTitle.DataSource = ds.Tables[0];
                    //grdMessages.PageSize = Convert.ToInt32(Session["AdminPageSize"]);
                    grdMessages.DataBind();
                    grdBlogTitle.DataBind();
                    return;
                }
                mgrobj = null;
            }
        //}
        //catch
        //{
        //    Response.Redirect(Session["strHttp"] + "/Community/Community.aspx");
        //}
    }
    protected void grdMessages_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdMessages.PageIndex = e.NewPageIndex;
        grdBlogTitle.PageIndex = e.NewPageIndex;

        BindGrid(Convert.ToInt32(Session["userID"].ToString()));
    }
    protected void grdBlogTitle_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdBlogTitle.PageIndex = e.NewPageIndex;
        grdMessages.PageIndex = e.NewPageIndex;

        BindGrid(Convert.ToInt32(Session["userID"].ToString()));
    }
}
